package com.xqboss.apps.service.nft;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.Brand;
import com.xqboss.apps.vo.nft.BrandVO;
import com.xqboss.apps.mapper.nft.BrandMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Administrator
 */
@Service
public class BrandService extends ServiceImpl<BrandMapper, Brand> {


    public List<BrandVO> getTreeList(Integer targetType) {
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        brandQueryWrapper.lambda().eq(Brand::getTargetType, targetType);
        List<Brand> brandList = this.list();

        List<BrandVO> brandVOList1 = new ArrayList<>();
        List<BrandVO> brandVOList2 = new ArrayList<>();
        List<BrandVO> brandVOList3 = new ArrayList<>();
        brandList.stream().forEach(brand -> {
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(brand, brandVO);
            if (brandVO.getLevel() == 1) {
                brandVOList1.add(brandVO);
            } else if (brandVO.getLevel() == 2) {
                brandVOList2.add(brandVO);
            } else if (brandVO.getLevel() == 3) {
                brandVOList3.add(brandVO);
            }
        });

        for (BrandVO brandVO2 : brandVOList2) {
            for (BrandVO brandVO3 : brandVOList2) {
                if (brandVO2.getId() == brandVO3.getParentId().longValue()) {
                    if (CollectionUtils.isEmpty(brandVO2.getChildren())) {
                        List<BrandVO> brandVOList = new ArrayList<>();
                        brandVOList.add(brandVO3);
                        brandVO2.setChildren(brandVOList);
                    } else {
                        if (brandVO3 != null) {
                            brandVO2.getChildren().add(brandVO3);
                        }
                    }
                }
            }
        }

        for (BrandVO brandVO1 : brandVOList1) {
            for (BrandVO brandVO2 : brandVOList2) {
                if (brandVO1.getId() == brandVO2.getParentId().longValue()) {
                    if (CollectionUtils.isEmpty(brandVO1.getChildren())) {
                        List<BrandVO> brandVOList = new ArrayList<>();
                        brandVOList.add(brandVO2);
                        brandVO1.setChildren(brandVOList);
                    } else {
                        if (brandVO2 != null) {
                            brandVO1.getChildren().add(brandVO2);
                        }
                    }
                }
            }
        }


        return brandVOList1;
    }


}
